Ext.ux.OptionPanel = function() {
	var desktop = MyDesktop.getDesktop();
	var win_option = desktop.getWindow('option-win');
	if (!win_option) {
		var _jsonReader1 = new Ext.data.JsonReader({
					root : 'results'
					//,totalProperty : 'totalCount'
				}, [{
							name : 'id',
							mapping : 'id'
						},{
							name : 'content',
							mapping : 'content'
						},{
							name : 'count',
							mapping : 'count'
						}]);
		var ds_option = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : '/admin/option.jhtml?method=getOptions&questionId='+publicCId
							}),
					reader : _jsonReader1
				});
		var cm_keyword = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(),
				new Ext.grid.CheckboxSelectionModel(), {
					header : "编号",
					width : 80,
					sortable : true,
					dataIndex : 'id'
				},{
					header : "选项",
					width : 200,
					sortable : true,
					dataIndex : 'content'
				},{
					header : "投票数",
					width : 100,
					sortable : true,
					dataIndex : 'count'
				}]);
		var grid_option = new Ext.grid.GridPanel({
			region : "center",
			cm : cm_keyword,
			sm : new Ext.grid.CheckboxSelectionModel(),
			border : false,
			loadMask : {
				msg : "数据加载中，请稍等..."
			},
			store : ds_option,
			viewConfig : {
				forceFit : true
			},
			tbar : [{
                    text: '添加',
                    tooltip: '添加选项',
                    iconCls: 'add',
                    handler: function(){
				// 开始创建Add面板
				var addCForm = new Ext.FormPanel( {
					layout :'form',
					border :false,
					labelSeparator :'：',
					frame :true,
					labelWidth :60,
					labelAlign :'left',
					buttonAlign :'right',
					bodyStyle :'padding:5px',
					width :330,
					fileUpload :true, // 允许上传文件
					enctype :'multipart/form-data',
					items : [ {
						xtype :'textfield',
						fieldLabel :'标题',
						name :'content',
						id:'content',
						maxLength :500,
						maxLengthText :'不能超过500个字符',
						allowBlank :false,
						blankText :'名称不能为空！',
						anchor :'96%'
					} ],
					buttons : [ {
						id :'btnSave',
						text :'保 存',
						handler : function() {
						
						if (addCForm.form.isValid())// 客户端先验证数据
						{
							//var kkk = encodeURIComponent(addCForm.findById('keyword').getValue());

							// this.disabled = true; //禁止保存按钮，防止多次提交
						addCForm.form.doAction('submit', {
							url :'/admin/option.jhtml?method=addOption',
						//	method :'get',
							params:{questionId:publicCId},
							waitTitle :'请稍候',
							waitMsg :'正在提交表单数据，请稍候...',
							success : function(form, action) {
								getAlertMessage("提示","操作成功!",Ext.MessageBox.WARNING);
								addCForm.form.reset();
								this.disabled = false;
								newC1win.close();
								
								ds_option.load();// 重新加载grid的数据
							},
							failure : function() {
								getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
								this.disabled = false;
							}
						})
					}
				}

					}, {
						text :'取 消',
						handler : function() {
						newC1win.close();
					}
					} ]
				});
			
					newC1win = new Ext.Window( {
						xtype :'window',
						title :'添加选项',
						buttonAlign :'right',
						closable :true,
						layout :'fit',
						modal :'true',
						width :350,
						height :150,
						closeAction :'close',
						plain :true,
						items :addCForm

					});
					newC1win.show();

			}
			},'-',{
					text : '修改',
					tooltip : '修改投票数',
					cls : 'x-btn-text-icon',
					icon : '/static/images/desktop/icon/165518105.gif',
					handler : function() {
						var rows = grid_option
								.getSelectionModel()
								.getSelections();// 返回值为
						if (rows.length == 0) {
							getAlertMessage("提示","最少选择一条信息，进行操作!",Ext.MessageBox.WARNING);
						} else {

							var editForm = new Ext.FormPanel(
									{
										layout : 'form',
										border : false,
										labelSeparator : '：',
										frame : true,
										labelWidth : 60,
										labelAlign : 'left',
										buttonAlign : 'right',
										bodyStyle : 'padding:5px',
										width : 230,
										fileUpload : true,
										enctype : 'multipart/form-data',
										reader : new Ext.data.JsonReader(
												{
													root : 'results'
												},
												[
														'id',
														'count']),
										items : [
												{
													xtype : 'hidden',
													name : 'id',
													anchor : '96%'
												},
												{
													xtype : 'textfield',
													fieldLabel : '票数',
													name : 'count',
													maxLength : 100,
													maxLengthText : '不能超过100个字符',
													allowBlank : false,
													blankText : '名称不能为空！',
													anchor : '96%'
												}],
										buttons : [
												{
													id : 'btnSave',
													text : '保 存',
													handler : function() {
														if (editForm.form
																.isValid()) {
															editForm.form
																	.doAction(
																			'submit',
																			{
																				url : '/admin/option.jhtml?method=updatePostCount',
																				method : 'post',
																				waitTitle : '请稍候',
																				waitMsg : '正在提交表单数据，请稍候...',
																				success : function(
																						form,
																						action) {
																							getAlertMessage("提示","操作成功!",Ext.MessageBox.WARNING);
																					editForm.form
																							.reset();
																					this.disabled = false;
																					editwin
																							.hide();
																							ds_option.load();// 重新加载grid的数据
																				},
																				failure : function() {
																					getAlertMessage("提示","保存失败!",Ext.MessageBox.WARNING);
																					this.disabled = false;
																				}
																			})
														}
													}
												},
												{
													text : '取 消',
													handler : function() {
														editwin
																.hide();
													}
												}]
									});
							editwin = new Ext.Window(
									{
										xtype : 'window',
										title : '修改投票数',
										buttonAlign : 'right',
										closable : true,
										layout : 'fit',
										modal : 'true',
										width : 250,
										height : 150,
										closeAction : 'hide',
										plain : true,
										items : editForm
									});
							var rows = grid_option
									.getSelectionModel()
									.getSelections();
							if (rows == 0) {
								getAlertMessage("提示","请确认是否选中要编辑的信息!",Ext.MessageBox.WARNING);
								return;
							}
							var id = rows[0]
									.get("id");
							editForm.form
									.load(
											{
												url : '/admin/option.jhtml?method=getOption',
												params : {
													questionId : id
												},
												method : 'post',
												success : function(
														form,
														action) {
												}
											});
							editwin
									.show();

						} // 弹出对话框警告
					}

					},'-',{
                
			text : '删除',
			tooltip : '挂起选中的一个',
			iconCls : 'remove',
			handler : function() {

				var rows = grid_option.getSelectionModel().getSelections();// 返回值为
				// Record
				// 数组
				if (rows.length == 0) {
					getAlertMessage("警告","最少选择一条信息，进行删除!",Ext.MessageBox.WARNING);
				} else {
					Ext.MessageBox.confirm('提示框', '您确定要进行该操作？', function(btn) {
						if (btn == 'yes') {

							if (rows) {
								for (var i = 0; i < rows.length; i++) {
									var conn = new Ext.data.Connection();
									conn.request({
										url : '/admin/option.jhtml?method=deleteOption',
										params : {
											optionId : rows[i]
													.get("id")
										}

									});

									break;
								}

							}
							loadDs(ds_option,20);
						}
					});
				} // 弹出对话框警告
			}
		
                },'-',{
                
															text : '查看内容',
															tooltip : '查看选中的一列',
															cls : 'x-btn-text-icon',
															icon : '/static/images/desktop/icon/16551842.gif',
															handler : function() {
																var rows = grid_option
																		.getSelectionModel()
																		.getSelections();// 返回值为
																if (rows.length == 0) {
																	getAlertMessage("提示","最少选择一条信息，进行操作!",Ext.MessageBox.WARNING);
																} else {
																	publicCId = rows[0].get("id");
																	loadClass("Ext.ux.OptionContentPanel","option-content-win");
																} // 弹出对话框警告
															}
														
                }]
		});
		win_option = desktop.createWindow({
					id : 'option-win',
					title : '选项管理',
					width : 740,
					height : 480,
					iconCls : 'icon-grid',
					shim : false,
					animCollapse : false,
					constrainHeader : true,
					layout : 'fit',
					items : grid_option
				});
		grid_option.store.load();
	}
	win_option.show();
}